// app/service/user.js
const Service = require('egg').Service;

class UserService extends Service {
    async getUser(uid) {
        let sql = `select * from user`
        const userRes = await this.ctx.app.mysql.query(sql);
        if (userRes.length > 0) {
            return {
                status: true,
                code: 200,
                message: "查询成功",
                data: userRes
            }
        } else {
            return {
                status: false,
                code: 403,
                message: "查询失败",
                data: []
            }
        }
    }

    async login(params) {
        const { username, password } = params
        let sql = `select username,nickname,age,phone,avatar,email,sex from user where username=? and password=?`
        let res = await this.ctx.app.mysql.query(sql, [username, password])
        if (res.length > 0) {
            return {
                status: true,
                code: 200,
                message: "账号密码正确",
                data: res[0]
            }
        } else {
            return {
                status: false,
                code: 403,
                message: "账号密码错误",
                data: {}
            }
        }
    }

    async register(params) {
        const { username, password, nickname, age, phone, avatar, email, sex } = params
        let sql = `select * from user where username=?`
        const userRes = await this.ctx.app.mysql.query(sql, [username]);
        if (userRes.length > 0) {
            return {
                code: 403,
                message: "账号已经存在",
                data: []
            }
        } else {
            let sql = `insert into user (username,password,nickname,age,phone,avatar,email,sex) values (?,?,?,?,?,?,?,?)`
            let Reguser = await this.ctx.app.mysql.query(sql, [username, password, nickname, age, phone, avatar, email, sex])
            if (Reguser.affectedRows = 1) {
                return {
                    status: true,
                    code: 200,
                    message: "注册成功",
                    data: Reguser[0]
                }
            } else {
                return {
                    status: false,
                    code: 403,
                    message: "注册失败",
                    data: {}
                }
            }
        }
    }

    async rolelist(params) {
        const { username, password } = params
        let sql = `SELECT * FROM user u JOIN role r JOIN auth a JOIN user_role_join ur JOIN role_auth_join ra ON u.id=ur.uid and r.id=ur.rid AND r.id=ra.rid AND a.id=ra.aid where username=? and password=?`
        const roleuser = await this.ctx.app.mysql.query(sql, [username, password]);
        if (roleuser.length > 0) {
            return {
                status: true,
                code: 200,
                message: "账号密码正确",
                data: roleuser
            }
        } else {
            return {
                status: false,
                code: 403,
                message: "账号密码错误",
                data: []
            }
        }
    }
}

module.exports = UserService;